package com.wxh.bomb.netty.thread.concurrent;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * CopyOnWirte容器，写操作时将当前容器复制一个副本进行操作，再将原来的容器的引用指向新的容器
 * 用于用在读多写少的场景使用
 *
 * @author wxh
 */
public class CopyOnWriteTest {
    private static CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    private static CopyOnWriteArrayList<String> set = new CopyOnWriteArrayList<String>();

    public static void main(String[] agrs) {
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    list.add("1");
                    list.add("2");
                    list.add("3");
                    list.add("4");
                    list.add("5");
                    list.add("6");
                    list.add("7");
                    list.add("8");
                    list.addIfAbsent("9");
                    list.addIfAbsent("10");

                }
            }).start();
        }

        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " " + list.size());
                }
            }).start();
        }

    }


}
